package com.zty.xiaomi.server.Controller;

import com.aliyun.tea.utils.StringUtils;
import com.zty.xiaomi.server.Entity.Message.message;
import com.zty.xiaomi.server.Entity.User;
import com.zty.xiaomi.server.Service.RegLogin.RegLogServiceImp;
import com.zty.xiaomi.server.Service.User.UserServiceImpl;
import com.zty.xiaomi.server.WebSocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController
@CrossOrigin
public class MessageController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WebSocketServer webSocketServer;
    @Autowired
    private RegLogServiceImp regLogServiceImp;

    @PostMapping("/message")
    public List<message> messageList(@RequestBody message message) {
        String key = null;
        if (message.getIdentity().equals("service")) {
            key = message.getTo() + "&" + message.getUsername();
        } else {
            key = message.getUsername() + "&" + message.getTo();
        }
        List<message> range = redisTemplate.opsForList().range(key, 0, -1);
        System.out.println(range);
        return range;
    }

    @PostMapping("/pushmessage")
    public String pushMessage() {
        webSocketServer.send("18150620291", "斤斤计较");
        return "l";
    }

    //    商家获取所有的信息列表
    @RequestMapping("/ServiceList")
    public List<User> ServiceList(String merchantsNo) {
        System.out.println(merchantsNo);
        List<User>userList=new ArrayList<>();
        if (StringUtils.isEmpty(merchantsNo)) {
            return userList;
        }
        Set<String> keys = redisTemplate.opsForHash().keys(merchantsNo);
        for (String s: keys
        ) {
            User userByUserName = regLogServiceImp.getUserByUserName(s);
            userList.add(userByUserName);
        }
        return userList;
    }


}
